/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com;

import java.awt.Graphics;
import java.awt.Point;
import javax.swing.JPanel;

/**
 *
 * @author alrz
 */
public class Grid extends JPanel
{

    private int size;
    public Figure[][] figuras;
    private Point puntoGuardar;
    
    public Grid(int size)
    {
        super();
        this.size = size;
        this.figuras = new Figure[size][size];
    }
    
    public void paint(Graphics g)
    {
        
        for(int i = 0; i<size; i++)
        {
            for(int j = 0; j<size; j++)
            {
                g.drawRect(50, 50, 100*(i+1), 100*(j+1));
            }
        }
    }
    
    public boolean verZona(Point p)
    {
        if(p.getX() < (size*100)+50 && p.getY() < (size*100)+50 && p.getX() > 50 && p.getY() > 50){
            int fila = (((p.y+50)/100)-1);
            int columna = (((p.x+50)/100)-1);

            this.puntoGuardar = new Point( fila, columna);
            System.out.println("x: " + p.x + " y: " + p.y);
            System.out.println("F: " + fila + " C: " + columna);
            //System.out.println("p: " + (int)(((p.getY() + 50)/ 100))+ " " + (int)((p.getX() + 50)/100));
            return true;
        }
        return false;
    }
    
    public boolean setFigure(Figure fig)
    {
        int x = puntoGuardar.x;
        int y = puntoGuardar.y;
        
        if(figuras[x][y] == null)
        {   
            if(x-1>=0 && this.figuras[x-1][y]!=null && this.figuras[x-1][y].getDown()!=fig.getUp())
            {
                return false;
            }
            if(x+1<size && this.figuras[x+1][y]!= null && this.figuras[x+1][y].getUp()!=fig.getDown())
            {
                return false;
            }
            if(-1+y>=0 && this.figuras[x][-1+y]!=null && this.figuras[x][y-1].getRight()!=fig.getLeft())
            {
                return false;
            }
            if(y+1<size && this.figuras[x][y+1]!=null && this.figuras[x][y+1].getLeft()!=fig.getRight())
            {
                return false;
            }
            
          
            System.out.println("x: " + x + " y: " + y );
            this.figuras[x][y] = fig;
            this.figuras[x][y].setBounds((y*100)+50,(x*100)+50, 100, 100);
            this.figuras[x][y].setPressed(false);
            return true;
        }
        return false;
    }
}
